import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import reactor.core.publisher.Flux;

public class Test {
    private static byte byteVal = 101;
    private static short shortVal = 10001;
    private static int intVal = 100000001;
    private static long longVal = 1000000000001L;

    private final static int BSIZE = Byte.SIZE;
    private final static int SSIZE = Short.SIZE;
    private final static int ISIZE = Integer.SIZE;
    private final static int LSIZE = Long.SIZE;

    private final static int BYTEMASK = 0xFF;

    public static String byteArrayToDecimalString(byte[] bArray) {
        StringBuilder rtn = new StringBuilder();
        for (byte b : bArray) {
            rtn.append(b & BYTEMASK).append(" ");
        }
        return rtn.toString();
    }

    /**
     * 对val进行编码
     *
     * @param dst    源字节数组
     * @param val    需要编码的long值
     * @param offset 编码起始的偏移量
     * @param size   编码的位数
     * @return
     */
    public static int encodeIntBigEndian(byte[] dst, long val, int offset, int size) {
        for (int i = 0; i < size; i++) {
            dst[offset++] = (byte) (val >> ((size - i - 1) * Byte.SIZE));
        }
        return offset;
    }

    /**
     * 对字节数组val进行解码
     *
     * @param val    源字节数组
     * @param offset 解码起始的偏移量
     * @param size   解码的位数
     * @return
     */
    public static long decodeIntBigEndian(byte[] val, int offset, int size) {
        long rtn = 0;
        for (int i = 0; i < size; i++) {
            rtn = (rtn << Byte.SIZE) | ((long) val[offset + i] & BYTEMASK);
        }
        return rtn;
    }

    public static void main(String args[]) {
        Flux.just(1,2,3)
            .flatMap(item->{
                return item==2?Flux.error(new RuntimeException("222")):Flux.just(item);
            }).onErrorResume(err->{
                return Flux.error(err);
        }).map(item->{
            return "-----------------"+item;
        }).subscribe((item)->{
            System.out.println("next");
            System.out.println(item);
        },(item)->{
            System.out.println("err");
            System.out.println(item);
        },()->{
            System.out.println("compline");
        });
        if(1==1){
            return;
        }
        String hexString = "5C0A656C768475286237";
        byte[] by = hexToByte(hexString);
        System.out.println(by.toString());
        try {
            System.out.println(decodeUnicode(new String(hexToByte(hexString))));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String msg = "002A00540050002C003800360039003700320037003000350030003400390030003500350034002C00520031002C0030003900330030003100310023";
        byte[] message = msg.getBytes();
        long rl = decodeIntBigEndian(message, 0, message.length);
        System.out.println(rl);
    }

    public static byte[] hexToByte(String hex) {

/**
 *先去掉16进制字符串的空格

 */

        hex = hex.replace(" ", "");

/**
 *字节数组长度为16进制字符串长度的一半

 */

        int byteLength = hex.length() / 2;

        byte[] bytes = new byte[byteLength];

        int m = 0;

        int n = 0;

        for (int i = 0; i < byteLength; i++) {

            m = i * 2 + 1;

            n = m + 1;

            int intHex = Integer.decode("0x" + hex.substring(i * 2, m) + hex.substring(m, n));

            bytes[i] = Byte.valueOf((byte) intVal);

        }

        return bytes;

    }

    /*
     * unicode编码转中文
     * 系统中接受中文参数变成百分号，如：“黄大”-->“%u9EC4%u5927”
     * 而实际上内容对应，应该是：“黄大”-->“\u9EC4\u5927”，中文变unicode
     */
    public static String decodeUnicode(String dataStr) {
        dataStr = dataStr.replace("%", "\\");//这行酌情不要
        int start = 0;
        int end = 0;
        final StringBuffer buffer = new StringBuffer();
        while (start > -1) {
            end = dataStr.indexOf("\\u", start + 2);
            String charStr = "";
            if (end == -1) {
                charStr = dataStr.substring(start + 2, dataStr.length());
            } else {
                charStr = dataStr.substring(start + 2, end);
            }
            char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
            buffer.append(new Character(letter).toString());
            start = end;
        }
        return buffer.toString();
    }
}


